package com.yangye.pisystem.service.impl;

import com.yangye.picommon.core.service.impl.BaseServiceImpl;
import com.yangye.pisystem.dao.SysUserRoleMapper;
import com.yangye.pisystem.domain.SysUserRole;
import com.yangye.pisystem.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangye
 * @date 2019/8/2
 */
@Service
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
	@Override
	@Transactional
	public int updateRoles(Long userId, List<Long> roleIds) {
		// 先删除再保存
		SysUserRole deleteModel = new SysUserRole();
		deleteModel.setUserId(userId);
		dao.delete(deleteModel);

		List<SysUserRole> saveList = new ArrayList<>();
		for (Long roleId : roleIds) {
			SysUserRole saveModel = new SysUserRole();
			saveModel.setRoleId(roleId);
			saveModel.setUserId(userId);
			saveList.add(saveModel);
		}
		return dao.insertList(saveList);
	}

	@Override
	public List<SysUserRole> getByUserId(Long userId) {
		SysUserRole queryModel = new SysUserRole();
		queryModel.setUserId(userId);
		return dao.select(queryModel);
	}
}
